package io.renren;

import io.renren.entity.DataSource;
import io.renren.utils.DateUtils;
import io.renren.utils.RRException;
import io.renren.utils.SettingConfig;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.sql.Date;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Set;

/**
 * @className: InitSources
 * @program: renren-generator
 * @description: 项目启动后初始化数据
 * @author: wzp12
 * @create: 2019-06-03 09:09
 **/
@Component
public class InitSources implements ApplicationRunner{
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println(MessageFormat.format("====={0}====初始化配置======", System.currentTimeMillis()));
        Configuration config = getConfig();

        SettingConfig.mainPath = config.getString("mainPath");
        SettingConfig.packageName = config.getString("package");
        SettingConfig.moduleName = config.getString("moduleName");
        SettingConfig.author = config.getString("author");
        SettingConfig.email = config.getString("email");
        SettingConfig.tablePrefix = config.getString("tablePrefix");
        SettingConfig.suffix = config.getString("suffix");

        // 初始化数据源
        DataSource dataSource = new DataSource();
        dataSource.setDriverClass(config.getString("jdbcDriverClass"));
        dataSource.setJdbcUrl(config.getString("url"));
        dataSource.setUser(config.getString("user"));
        dataSource.setPassword(config.getString("password"));
        SettingConfig.defaultSource = dataSource;
        Set<DataSource> dataSourceSet = new HashSet<DataSource>(){{
            add(dataSource);
        }};

        SettingConfig.sourceSet = dataSourceSet;

    }



    /**
     * 获取配置信息
     */
    public static Configuration getConfig() {
        try {
            return new PropertiesConfiguration("generator.properties");
        } catch (ConfigurationException e) {
            throw new RRException("获取配置文件失败，", e);
        }
    }
}
